package com.xmut.bs.handaccount.widget;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wyt.searchbox.SearchFragment;
import com.wyt.searchbox.custom.IOnSearchClickListener;
import com.xmut.bs.handaccount.R;

/**
 * Description
 * Created by Dollkey on 2018/4/7.
 */

public class SearchBarView extends ConstraintLayout {

    private final ImageView mRightIv;
    private final TextView mSearchTv;
    private SearchFragment mSearchFragment;

    public SearchBarView(Context context) {
        this(context, null);
    }

    public SearchBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchBarView(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.common_search_bar, this);
        mRightIv = view.findViewById(R.id.right_iv);
        mSearchTv = view.findViewById(R.id.search_hint);
        mSearchFragment = SearchFragment.newInstance();
        view.findViewById(R.id.search_bar).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchFragment.show(((AppCompatActivity) context).getSupportFragmentManager(), SearchFragment.TAG);
            }
        });
    }

    public void setRightIvSrc(int resId){
        mRightIv.setImageResource(resId);
    }

    public void hideRightIv(){
        mRightIv.setVisibility(GONE);
    }

    public void setHint(CharSequence hint){
        mSearchTv.setText(hint);
    }

    public void setOnRightClickListener(OnClickListener onClickListener) {
        mRightIv.setOnClickListener(onClickListener);
    }

    public void setIOnSearchListener(IOnSearchClickListener IOnSearchListener) {
        mSearchFragment.setOnSearchClickListener(IOnSearchListener);
    }

}
